package com.xspace.android.imcommon.interfaces;

import com.xspace.android.imcommon.entity.DBSMSBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MsmMg implements IToSMSListener {
	private ServiceManager mServiceManager = null;
	private Map<String, IToSMSListener> mListenerMap = new HashMap<String, IToSMSListener>();
	private IToSMSListener mUnReadListenerMap;
	private MsmMg() {
		mServiceManager = ImServiceConnection.getInstance().getImServiceConnection();
	}

	private static MsmMg instance = null;

	public static MsmMg getInstance() {
		if (null == instance) {
			instance = new MsmMg();
		}
		return instance;
	}

	public void initSms(String smsType, IToSMSListener mListener) {
		if (mListener != null) {
			mListenerMap.put(smsType, mListener);
			try {
				mServiceManager.initSms(MsmMg.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	
	public void initUnReadSms(IToSMSListener mListener) {
		if (mListener != null) {
			mUnReadListenerMap=mListener;
			try {
				mServiceManager.initSms(MsmMg.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public ArrayList<DBSMSBean> querySmsList(String smsType, String jid) {
		ArrayList<DBSMSBean> mList = new ArrayList<DBSMSBean>();
		try {
			SmsListAidl mSmsListAidl = mServiceManager.querySmsList(smsType, jid);
			if (mSmsListAidl != null) {
				mList.addAll(mSmsListAidl.respList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mList;
	}

	public void deleteSmsInfo(String smsType, int id) {
		try {
			mServiceManager.deleteSmsInfo(smsType, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateSmsRead(String smsType, String jid) {
		try {
			mServiceManager.updateSmsRead(smsType, jid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void unReadQury(String jid) {
		try {
			mServiceManager.unReadQury(jid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onToUnRead(int unreadSize, String smsType) {
		// TODO Auto-generated method stub
		try {
			mUnReadListenerMap.onToUnRead(unreadSize, smsType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onToSMSInfo(DBSMSBean info, String smsType) {
		// TODO Auto-generated method stub
		try {
			mListenerMap.get(smsType).onToSMSInfo(info, smsType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}